package com.wtwd.campus.vcom.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.entity.MqPhoneDatas;
import com.wtwd.campus.entity.PhoneData;
import com.wtwd.campus.service.CustomerDealCmdService;
import com.wtwd.campus.vcom.common.Constant;
import com.wtwd.campus.vcom.dao.VcomPhoneMapper;
import com.wtwd.campus.vcom.entity.PhoneDataMap;
import com.wtwd.campus.vcom.entity.VcomPhoneData;
import com.wtwd.campus.vcom.service.RequestRecordService;
import com.wtwd.campus.vcom.service.VcomPhoneService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2022/02/28
 * @description
 */
@Service
public class VcomPhoneServiceImpl implements VcomPhoneService {

    @Autowired
    private VcomPhoneMapper vcomPhoneMapper;

    @Resource
    private RequestRecordService recordService;


    @Override
    public Boolean fallBackPhone(String imei, String commandNumber) {
        VcomPhoneData vcomPhoneData = vcomPhoneMapper.getPhoneListByImeiAndCommandnumber(imei, commandNumber);
        if (vcomPhoneData != null) {//还原数据(旧数据还原，以及状态改为正常)
            vcomPhoneData.setPhoneData(vcomPhoneData.getOldData());
            vcomPhoneData.setStatus(Constant.PHONE_STATUS_NORMAL);
            int i = vcomPhoneMapper.modifyPhoneRecord(vcomPhoneData);
            return i > 0 ? true : false;
        }
        return false;
    }

    @Override
    public VcomPhoneData getVcomPhoneByImeiAndCommandNumber(String imei, String commandNumber) {
        return vcomPhoneMapper.getPhoneListByImeiAndCommandnumber(imei, commandNumber);
    }

    @Override
    public Integer updateVcomRequestStateByTypesAndImei(String imei, List<Integer> types, List<Integer> states, int state) {
        if (types != null && types.size() > 0 && states != null && states.size() > 0) {
            return recordService.updateVcomRequestStateByTypesAndImei(imei, types, states, state);
        }
        return 0;
    }

    @Override
    public Integer updatePhoneDateByImei(String imei, String type, MqPhoneDatas phoneDatas) {
        Integer dataType = Integer.valueOf(type);
        if (CustomerDealCmdService.ALL_PHONE_TYPE.equals(type)) {//暂时只针对所有号码类型做处理
            List<PhoneData> datas = phoneDatas.getPhoneDatas();
            List<PhoneDataMap> sosList = new ArrayList<>();
            datas.stream().filter(e -> e.getPhoneType().equals(Constant.PHONE_SOS))
                    .forEach(e -> {
                        PhoneDataMap phoneDataMap = new PhoneDataMap();
                        phoneDataMap.setName(e.getName());
                        phoneDataMap.setPhone(e.getPhone());
                        phoneDataMap.setSequence(Integer.valueOf(e.getKey()));
                        sosList.add(phoneDataMap);
                    });
            List<PhoneDataMap> familyList = new ArrayList<>();
            datas.stream().filter(e -> e.getPhoneType().equals(Constant.PHONE_FAMILY)).forEach(
                    e -> {
                        PhoneDataMap phoneDataMap = new PhoneDataMap();
                        phoneDataMap.setName(e.getName());
                        phoneDataMap.setPhone(e.getPhone());
                        phoneDataMap.setSequence(Integer.valueOf(e.getKey()));
                        familyList.add(phoneDataMap);
                    }
            );
            List<PhoneDataMap> whiteList = new ArrayList<>();
            datas.stream().filter(e -> e.getPhoneType().equals(Constant.PHONE_WHITE)).forEach(
                    e -> {
                        PhoneDataMap phoneDataMap = new PhoneDataMap();
                        phoneDataMap.setName(e.getName());
                        phoneDataMap.setPhone(e.getPhone());
                        phoneDataMap.setSequence(Integer.valueOf(e.getKey()));
                        whiteList.add(phoneDataMap);
                    });
            insertOrUpdatePhoneData(imei, dataType, phoneDatas.getCommandNumber(), sosList);
            insertOrUpdatePhoneData(imei, dataType, phoneDatas.getCommandNumber(), familyList);
            insertOrUpdatePhoneData(imei, dataType, phoneDatas.getCommandNumber(), whiteList);
        }
        return 0;
    }

    @Override
    public Integer insertOrUpdatePhoneData(String imei, Integer type, String commandNumber, List<PhoneDataMap> phoneDataMaps) {
        VcomPhoneData phone = vcomPhoneMapper.getPhoneListByType(imei, type);
        Integer rest = 0;
        if (phone != null) {
            phone.setOldData(phone.getPhoneData());
            phone.setPhoneData(JSONObject.toJSONString(phoneDataMaps));
            phone.setUpdateTime(new Date().getTime());
            phone.setCommandNumber(commandNumber);
            phone.setType(type);
            phone.setStatus(Constant.PHONE_STATUS_NORMAL);
            rest = vcomPhoneMapper.modifyPhoneRecord(phone);
        } else {
            phone = new VcomPhoneData();
            phone.setImei(imei);
            phone.setPhoneData(JSONObject.toJSONString(phoneDataMaps));
            phone.setCommandNumber(commandNumber);
            phone.setUpdateTime(new Date().getTime());
            phone.setType(type);
            phone.setStatus(Constant.PHONE_STATUS_NORMAL);
            rest = vcomPhoneMapper.addPhoneRecord(phone);
        }
        return rest;
    }

    @Override
    public boolean deletePhone(String imei) {
        if (StringUtils.isBlank(imei)){
            return false;
        }
        return vcomPhoneMapper.deletePhone(imei)>0?true:false;
    }
}
